package com.vfa.vocaburay;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.vfa.test.AnswerE;
import com.vfa.test.QuestionData;
import com.vfa.test.QuestionE;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class FinalTest_DynamicLayout extends Activity{
	public static int index = 0;
	LinearLayout linear1;
	TextView question;
	LinearLayout linear2;
	RadioButton rb;
	RadioGroup rdg;
	LinearLayout linear3;
	Button btPrePage, btNext, btFinish;
	TextView tv;
	TextView timer;
	
	QuestionData questiondata;
	int questionid;
	private ArrayList<QuestionE> listQuestion ;
	private ArrayList<AnswerE> listAnswer ;
	String strquestion;
	Map<Integer, Integer> status = new HashMap<Integer, Integer>();
	ArrayList<Integer> listID = new ArrayList<Integer>();
	
	public class MyCounter extends CountDownTimer{

		public MyCounter(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
			
		}

		@Override
		public void onFinish() {
			timer.setText("Time Compeleted");
			Intent i =new Intent(FinalTest_DynamicLayout.this, ResultTest.class);
			startActivity(i);
			finish();
		}
		@Override
		public void onTick(long millisUntilFinished) {
			timer.setText(millisUntilFinished/1000+"");
		}	
	}
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		try{
			String destPath = "data/data/com.vfa.vocaburay/databases/Question.sqlite";
			File f = new File(destPath);
			if(!f.exists()){
				CopyDB(getBaseContext().getAssets().open("Question.sqlite"),
					new FileOutputStream(destPath));
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		
		
		listQuestion = new ArrayList<QuestionE>();
		questiondata = new QuestionData(this);
		getQuestion(FinalTest_DynamicLayout.index);
		getAnswer(questionid);
		
		linear1 = new LinearLayout(this);
		linear1.setOrientation(LinearLayout.VERTICAL);
		tv = new TextView(this);
		tv.setText("Final Test");
		linear1.addView(tv);
		timer = new TextView(this);
		linear1.addView(timer);
		linear2 = new LinearLayout(this);
		linear2.setOrientation(LinearLayout.VERTICAL);
		rdg = new RadioGroup(this);
		//rdg.seto
		linear2.addView(rdg);
		for(int i = 0; i < listAnswer.size(); i++){
			RadioButton rb = new RadioButton(this);
			rb.setText(listAnswer.get(i).getAnswer());
			rdg.addView(rb);
		}
		returnChecked();
		Log.i("Checked", ""+returnChecked());
	
		
		linear3 = new LinearLayout(this);
		linear3.setOrientation(LinearLayout.HORIZONTAL);
		btFinish = new Button(this);
		btFinish.setText("Finish");
		btFinish.setEnabled(false);
		btNext = new Button(this);
		btNext.setText("Next Question");
		btPrePage = new Button(this);
		btPrePage.setText("Pre Quetion");
		btPrePage.setEnabled(false);
		linear3.addView(btPrePage);
		linear3.addView(btFinish);
		linear3.addView(btNext);
		question = new TextView(this);
		question.setHint("Question is loaded here");
		question.setText(strquestion);
		linear1.addView(question);
		linear1.addView(linear2);
		linear1.addView(linear3);
		
		setContentView(linear1);
		
		btNext.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SaveStatus();
				Log.i("Checked", ""+returnChecked());
				index++;
				getQuestion(FinalTest_DynamicLayout.index);
				getAnswer(questionid);
				question.setText(strquestion);
				linear2.removeView(rdg);
				rdg = new RadioGroup(FinalTest_DynamicLayout.this);
				for(int i = 0; i < listAnswer.size(); i++){
					RadioButton rb = new RadioButton(FinalTest_DynamicLayout.this);
					rb.setText(listAnswer.get(i).getAnswer());
					rdg.addView(rb);
					if(rb.isChecked()){
						Log.i("status", ""+i);
					}
					
				}
				linear2.addView(rdg);

				if(index > 0){
					btPrePage.setEnabled(true);
				}
				if(index == listQuestion.size() -1 ){
					btNext.setEnabled(false);
					btFinish.setEnabled(true);
				}
				Log.i("QsId", ""+questionid);
				
				
			}
		});
		
		btPrePage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				index--;
				getQuestion(FinalTest_DynamicLayout.index);
				getAnswer(questionid);
				question.setText(strquestion);
				
				linear2.removeView(rdg);
				rdg = new RadioGroup(FinalTest_DynamicLayout.this);
				for(int i = 0; i < listAnswer.size(); i++){
					RadioButton rb = new RadioButton(FinalTest_DynamicLayout.this);
					rb.setText(listAnswer.get(i).getAnswer());
					rdg.addView(rb);
				}
				linear2.addView(rdg);

				if(index == 0){
					btPrePage.setEnabled(false);
				}
				if(index < listQuestion.size()-1){
					btNext.setEnabled(true);
				}
			}
		});
		
		btFinish.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(FinalTest_DynamicLayout.this);
				builder.setMessage("Are you sure you want to finish the test?");
				builder.setCancelable(false);
				builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent i =new Intent(FinalTest_DynamicLayout.this, ResultTest.class);
						startActivity(i);
						finish();
					}
				});
				builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).show();
			}
		});

	}
	



	private void CopyDB(InputStream inputStream, FileOutputStream fileOutputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int length;
		while((length = inputStream.read(buffer)) > 0){
			fileOutputStream.write(	buffer, 0, length);
		}
		inputStream.close();
		fileOutputStream.close();
		
	}




	@Override
	protected void onStart() {
		super.onStart();
		final MyCounter timer = new MyCounter (90000, 1000);
		timer.start();
	}
	
	private void getQuestion(int index){
		listQuestion = QuestionData.getQuestion(0);
		strquestion = listQuestion.get(index).getQuestion();
		questionid = listQuestion.get(index).getQuestionId();
	}
		
	private void getAnswer(int idquestion){
		listAnswer = new ArrayList<AnswerE>();
		listAnswer = QuestionData.getAnswer(idquestion);
		
	}
	
	public int returnChecked(){
		int isCheck = 0;
		for (int i = 0; i < listAnswer.size(); i++){
			if(rb.isChecked())
				return i;
		}
		return isCheck;
	}
	
	private void SaveStatus(){
		listID.add(questionid);
		status.put(questionid, returnChecked());
	}
	
}
